package com.ruoyi.dgw.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dgw.domain.TagNumberExecl;
import com.ruoyi.dgw.domain.Vaccine;
import com.ruoyi.dgw.mapper.VaccineMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.dgw.mapper.TagNumberMapper;
import com.ruoyi.dgw.domain.TagNumber;
import com.ruoyi.dgw.service.ITagNumberService;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;

/**
 * 耳标Service业务层处理
 *
 * @author l
 * @date 2021-12-03
 */
@Service
@Slf4j
public class TagNumberServiceImpl implements ITagNumberService {
    @Resource
    private TagNumberMapper tagNumberMapper;

    @Resource
    private SysDeptMapper sysDeptMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private VaccineMapper vaccineMapper;

    /**
     * 查询耳标
     *
     * @param tagNumberId 耳标主键
     * @return 耳标
     */
    @Override
    public TagNumber selectTagNumberByTagNumberId(String tagNumberId) {
        return tagNumberMapper.selectTagNumberByTagNumberId(tagNumberId);
    }

    /**
     * 查询耳标列表
     *
     * @param tagNumber 耳标
     * @return 耳标
     */
    @Override
    public List<TagNumber> selectTagNumberList(TagNumber tagNumber) {
        return tagNumberMapper.selectTagNumberList(tagNumber);
    }

    /**
     * 新增耳标
     *
     * @param tagNumber 耳标
     * @return 结果
     */
    @Override
    public int insertTagNumber(TagNumber tagNumber) {
        tagNumber.setCreateTime(DateUtils.getNowDate());
        return tagNumberMapper.insertTagNumber(tagNumber);
    }

    /**
     * 修改耳标
     *
     * @param tagNumber 耳标
     * @return 结果
     */
    @Override
    public int updateTagNumber(TagNumber tagNumber) {
        return tagNumberMapper.updateTagNumber(tagNumber);
    }

    /**
     * 批量删除耳标
     *
     * @param tagNumberIds 需要删除的耳标主键
     * @return 结果
     */
    @Override
    public int deleteTagNumberByTagNumberIds(String[] tagNumberIds) {
        return tagNumberMapper.deleteTagNumberByTagNumberIds(tagNumberIds);
    }

    /**
     * 删除耳标信息
     *
     * @param tagNumberId 耳标主键
     * @return 结果
     */
    @Override
    public int deleteTagNumberByTagNumberId(String tagNumberId) {
        return tagNumberMapper.deleteTagNumberByTagNumberId(tagNumberId);
    }

    /**
     * 根据耳标查询耳标数据
     *
     * @param earNumber
     * @return
     */
    @Override
    public TagNumber selectTagNumberByTagNumber(String earNumber, String userId) {
        return tagNumberMapper.selectTagNumberByTagNumber(earNumber, userId);
    }

    /**
     * 根据耳标修改报废
     *
     * @param earNumber
     */
    @Override
    public void updateEarNumber(String earNumber) {
        tagNumberMapper.updateEarNumber(earNumber);
    }

    @Override
    public void updateEar(String EarNumber, String earNumber) {
        tagNumberMapper.updateEar(EarNumber,earNumber);
    }

    /**
     * 导入耳标数据
     *
     * @param list     用户数据列表
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importTagNumber(List<TagNumber> list, String operName) {
        // tagNumberId=null, earNumber=GS00011, userId=null, phone=13266682008, isUse=null, enterpriseId=null, insuranceId=null, loans=null, deptId=null, name=正新, deptName=文水, scrap=null
        log.info("导入的数据：{}",list);

        Vaccine vaccine = new Vaccine();
        vaccine.setAftosa(0L);
        vaccine.setAnthrax(0L);
        vaccine.setClothbacteria(0L);
        //在要回滚的地方调用下面的方法就可以了
        list.forEach(tagNumberList -> {
            int deptId = sysDeptMapper.selectDeptByName(tagNumberList.getDeptName());
            int userId = sysUserMapper.queryUserById(tagNumberList.getName(),tagNumberList.getPhone());
         //   SimpleDateFormat simpleDateFormat = new SimpleDateFormat(tagNumberList.getCowTime(),"YYYY-MM");
            vaccine.setUserId(Convert.toLong(userId));
            vaccine.setBreedName(tagNumberList.getName());
            vaccine.setEarNumber(tagNumberList.getEarNumber());
            vaccineMapper.insertVaccine(vaccine);
            tagNumberList.setDeptId(Convert.toLong(deptId));
            tagNumberList.setInsuranceId("0");
            tagNumberList.setLoans(0L);
            tagNumberList.setScrap(1L);
            tagNumberList.setCreateTime(DateUtils.getNowDate());
            tagNumberList.setUserId(Convert.toStr(userId));
            tagNumberMapper.insertTagNumber(tagNumberList);
        });

        return "操作成功";
    }

    @Override
    public List<TagNumber> queryBX(TagNumber tagNumber) {
        return tagNumberMapper.queryBX(tagNumber);
    }


}
